<?php

class BaseController
{
    protected $dataRoot = __DIR__ . '/data';
    protected $orderDataRoot = __DIR__ . '/data';

    function initialize()
    {
        if (!file_exists($this->dataRoot)) {
            mkdir($this->dataRoot, 0777, true);
        }
        if (!file_exists($this->orderDataRoot)) {
            mkdir($this->orderDataRoot, 0777, true);
        }
    }

    function index()
    {
        $list = [];
        $fsi = new FilesystemIterator($this->orderDataRoot);
        foreach ($fsi as $file) {
            if ($file->isDir() || $file->getExtension() != 'json' || !is_numeric(basename($file->getFilename(), '.json'))) {
                continue;
            }
            $list[] = $file->getBasename('.' . $file->getExtension());
        }
        return message($list);
    }

    public function read($id)
    {
        $filename = $this->orderDataRoot . $id . '.json';
        if (!file_exists($filename)) {
            return message(null, '数据不存在', 404);
        }
        $data = file_get_contents($filename);
        if ($data) {
            $data = json_decode(trim($data), true);
        }
        return message($data);
    }

    function save()
    {
        $issue = get_param('issue');
        if ($issue) {
            // $drawalInfo = get_param('DrawalInfo');
            $aiList = get_param('AiList');
            $autoList = get_param('AutoList');
            $betterAutoList = get_param('BetterAutoList');
        } else {
            try {
                $formData = file_get_contents('php://input');
                if (!$formData) {
                    return message(null, '提交数据无效', 500);
                }
                $formData = json_decode($formData, true);
                $issue = $formData['issue'] ?? '';
                // $drawalInfo = $data['DrawalInfo'] ?? '';
                $aiList = $formData['AiList'] ?? '';
                $autoList = $formData['AutoList'] ?? '';
                $betterAutoList = $formData['BetterAutoList'] ?? '';
                unset($formData);
            } catch (\Exception $ex) {
                return message(null, $ex->getMessage(), 500);
            }
        }
        $filename = $this->orderDataRoot . $issue . '.json';
        $data = [];
        if (file_exists($filename)) {
            $data = file_get_contents($filename);
            if ($data) {
                $data = json_decode(trim($data), true);
            }
        }
        if (!$data) {
            $data = [
                'DrawalInfo' => [
                    'lotteryDrawNum' => $issue,
                    'lotteryDrawTime' => '',
                    'lotteryDrawResult' => '',
                ],
                'AiList' => [],
                'AutoList' => [],
                'BetterAutoList' => []
            ];
        }
        if ($aiList) {
            foreach ($aiList as $key => $value) {
                $data['AiList'][$key] = $value;
            }
        }
        if ($autoList) {
            $data['AutoList'] = $autoList;
        }
        if ($betterAutoList) {
            $data['BetterAutoList'] = $betterAutoList;
        }
        file_put_contents($filename, json_encode($data, JSON_UNESCAPED_UNICODE));
        return message($data);
    }
}
